package assistant

import (
	"context"

	"paper-pro/app/assistant/api/internal/svc"
	"paper-pro/app/assistant/api/internal/types"
	"paper-pro/pkg/ctxdata"
	assistantclient "paper-pro/pkg/rpc/assistant"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// get assistant
func NewGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLogic {
	return &GetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetLogic) Get(req *types.IdReq) (resp *types.AssistantInfo, err error) {
	userId, err := ctxdata.GetUidFromCtx(l.ctx)
	if err != nil {
		return nil, errors.Wrapf(err, "Get Assistant: get UserId error: %v", err)
	}

	rpcResp, err := l.svcCtx.AssistantRpc.GetAssistant(l.ctx, &assistantclient.GetAssistantReq{
		Id:     req.Id,
		UserId: userId,
	})
	if err != nil {
		return nil, errors.Wrapf(err, "Get Assistant: AssistantRpc GetAssistant error: %v", err)
	}

	var assistantInfo types.AssistantInfo
	err = copier.CopyWithOption(&assistantInfo, rpcResp.AssistantInfo, copier.Option{DeepCopy: true})
	if err != nil {
		return nil, errors.Wrapf(err, "Get Assistant: 数据转换失败，error: %v", err)
	}

	return &assistantInfo, nil
}
